/**
 * @fileoverview   anim/heart  Anim核心模块
 * Created by fu.
 * Date: 12-2-8
 * Time: 下午5:43
 */
F.reg('anim/heart', function (f, anim) {
    // background-position
    //  color  background-color
    // border padding margin
    // fontSize width height left right top bottom

    var space = /\S+/g, transformable = [
        'backgroundPositionX', 'backgroundPositionY',
        'opacity',
        'borderLeftWidth', 'borderRightWidth', 'borderBottomWidth', 'borderTopWidth',
        'paddingLeft', 'paddingRight', 'paddingTop', 'marginBottom',
        'marginLeft', 'marginRight', 'marginTop', 'marginBottom',
        'fontSize', 'width', 'height', 'left', 'right', 'top', 'bottom',
        'scrollLeft','scrollTop'
    ];
    if (anim.color) {
        transformable = f.merge(transformable,
            ['color', 'backgroundColor', 'borderLeftColor', 'borderRightColor', 'borderBottomColor', 'borderTopColor']);
    }
    function iscolor(v) {
        return v.toLowerCase().indexOf('color') > -1;
    }

    f.extend(anim, {
        /**

         * @name F.Anim.prototype.run
         * @function
         * @namespace 执行动画，可以链式调用，动画将会加入队列
         * @param {Object} ops 必选 动画执行选项 {left:100}
         * @param {Number} druation 可选  持续时间 默认 500
         * @param {String} easing 可选 缓动效果  默认 easeInOutQuad
         * @param {Function} fn 可选 执行完调用的函数
         * @example
         * 例子：
         * 1.
         * new anim('a').run({left:100},function(){
         *     alert('left:100');
         * }).run({top:100},1000,'linear')
         * //动画会先向左移到100，再向上移动100、
         */

        run:function (ops) {
            var tmpopt,
                _this = this;


            if (ops) {
                tmpopt = {
                    easing:'easeInOutQuad',
                    fn:function () {
                    },
                    opt:{},
                    druation:500
                };
                f.each(arguments, function (arg) {
                    switch (f.type(arg)) {
                        case 'string':
                            tmpopt.easing = arg;
                            break;
                        case 'function':
                            tmpopt.fn = arg;
                            break;
                        case 'object':
                             tmpopt.opt = arg;
                            break;
                        case 'number':
                            tmpopt.druation = arg;
                            break;
                    }
                });
                _this.queue.push(tmpopt);
            }
            //如果动画未结束、返回
            if (!_this.isend) {
                return _this;
            }
            //动画结束、 取出下一个动画
            _this.curopt = _this.queue.shift();

            //处理一下 key
            f.each(_this.curopt.opt, function (val, key, opt) {
                var valarr;
                switch (key) {
                    case 'border':
                    {
                        valarr = val.match(space);
                        delete opt[key];
                        f.each(valarr, function (v) {
                            if (v.indexOf('px') > -1) {
                                v = parseFloat(v);
                                opt['borderLeftWidth'] = v;
                                opt['borderRightWidth'] = v;
                                opt['borderBottomWidth'] = v;
                                opt['borderTopWidth'] = v;
                            } else if (v.indexOf('#') > -1) {
                                opt['borderLeftColor'] = v;
                                opt['borderRightColor'] = v;
                                opt['borderBottomColor'] = v;
                                opt['borderTopColor'] = v;
                            } else {
                                opt['borderStyle'] = v;
                            }
                        });
                        break;
                    }
                    case 'padding':
                    {
                        delete opt[key];
                        opt['paddingLeft'] = val;
                        opt['paddingRight'] = val;
                        opt['paddingTop'] = val;
                        opt['paddingBottom'] = val;
                        break;
                    }
                    case 'margin':
                    {
                        delete opt[key];
                        opt['marginLeft'] = val;
                        opt['marginRight'] = val;
                        opt['marginTop'] = val;
                        opt['marginBottom'] = val;
                        break;
                    }
                    case 'backgroundPosition':
                    {
                        delete opt[key];
                        valarr = val.match(space);
                        opt['backgroundPositionX'] = parseFloat(valarr[0]);
                        opt['backgroundPositionY'] = parseFloat(valarr[1]);
                    }
                }

            });
            f.each(_this.curopt.opt, function (val, key, opt) {
                if (f.inarray(transformable, key) === -1) {
                    _this.elem.each(function () {
                        f.each(opt, function () {
                            this.css(key, val);
                        }, this);

                    });

                    delete  opt[key];
                }
            });
            _this.play();

            return _this;
        },
        /**

         * @name F.Anim.prototype.stop
         * @function
         * @namespace 停止动画、直接停止到当前队列的最后样式，或所有队列的最后样式
         * @param {Boolean} end true时、直接停止到队列最后一个

         * @example
         * 例子：
         * 1.
         * var a=new anim('a').run({left:100},function(){
         *     alert('left:100');
         * }) ;
         *  a.stop();
         */

        stop:function (end) {
            var _this = this, opt = [_this.curopt.opt];
            clearTimeout(_this.timeID);
            if (end === true) {
                f.each(_this.queue, function (q) {
                    opt.push(q.opt);
                });
            }
            _this.elem.each(function () {
                f.each(opt, function (o) {
                    this.css(o);
                }, this);

            });
            this.isend = true;

        },
        /**

                 * @name F.Anim.prototype.pause
                 * @function
                 * @namespace 暂停动画

                 * @example
                 * 例子：
                 * 1.
                 * var a=new anim('a').run({left:100},function(){
                 *     alert('left:100');
                 * }) ;
                 *  a.pause();
                 */
        pause:function () {
            clearTimeout(this.timeID);
            this.isend = true;

        },
        /**

         * @name F.Anim.prototype.play
         * @function
         * @namespace 播放动画、动画暂停后，可播放继续

         * @example
         * 例子：
         * 1.
         * var a=new anim('a').run({left:100},function(){
         *     alert('left:100');
         * }) ;
         *  a.play();
         */
        play:function () {
            var _this = this,
                elem = _this.elem,
                basestyle = [],
                changestyle = [],
                passtime = 15;
            elem.each(function (ele, i) {
                basestyle[i] = {};
                changestyle[i] = {};
                f.each(_this.curopt.opt, function (val, key, opt) {
                    // background-color background-position
                    //  color font-size  border padding margin width height left right top bottom
                     //0-255,256-510,511-765

                    var tmpcssval;
                    if (iscolor(key)) {
                        basestyle[i][key] = this.css(key);
                        changestyle[i][key] = opt[key];
                    } else {
                        tmpcssval = parseFloat(this.css(key));
                        basestyle[i][key] = tmpcssval;
                        changestyle[i][key] = opt[key] - tmpcssval;
                    }
                 }, this);
            });
            _this.laststyle = basestyle;
            _this._move(elem, basestyle, changestyle, _this.curtime, _this.curopt.druation, anim.tween[_this.curopt.easing], passtime, _this.curopt);
        },
        _move:function (elem, basestyle, changestyle, time, druation, tween, passtime, curopt) {

            var _this = this,
                selfarg = arguments,
                selfn = arguments.callee;
            _this.isend = false;
            _this.curtime = time;

            //增加时间
            time += passtime;
            //动画结束
            if (time >= druation) {
                _this.curtime = 0;
                //设置到结果
                elem.each(function () {
                    this.css(curopt.opt);
                });
                curopt.fn.call(this.elem);
                _this.isend = true;
                if (_this.queue.length > 0) {
                    _this.run();
                }
                return;
            }
            _this.timeID = setTimeout(function () {
                //循环每个元素
                elem.each(function (ele, i) {
                    var curstyle = {};
                    //循环样式，获得当前样式
                    f.each(changestyle[i], function (val, key) {

                        curstyle[key] = iscolor(key) ?
                            anim.color.tween(tween, time, basestyle[i][key], changestyle[i][key], druation) :
                            tween(time, basestyle[i][key], changestyle[i][key], druation);
                    }, this);
                    this.css(curstyle);
                });
                selfn.apply(_this, selfarg);

            }, passtime);
        }

    });
    return anim;
}, {
    requires:[

    ]
});